<?php



if (isset($_GET["test_db"]))
{
	include_once($_SERVER["DOCUMENT_ROOT"] . "/core/inc.php");
}
define("CACHE_PREFIX_user_follow","_user_follow_");
class db_user_follow extends db_base
{
	var $tbl_name = "user_follow";

	function db_user_follow()
	{
		parent::db_base();
	}

//前台程序开始	
	/**
	 * 前台显示单个数据
	 * @param int id
	 */
	function get_item_by_uid_and_follow_uid($uid,$follow_uid)
	{       
		
			 
		$data = false;
		$key = CACHE_PREFIX_user_follow . "-get_item_by_uid_and_follow_uid-{$uid}-{$follow_uid}";
		$mc = mem_cache::get_instance();
		$cache = $mc->get($key);
		if ($cache === false)
		{
			$uid = intval($uid);
			$follow_uid = intval($follow_uid);
			
			//处理where条件
			$where = " uid = {$uid} and  follow_uid = $follow_uid and status = 1 " ;	
			$data = $this->get_alllist($this->tbl_name, 1, 1, "",$where);
			$mc->set($key, serialize($data), 0, CACHE_EXPIRE_MIN_5);
		}
		else
		{
			$data = unserialize($cache);
		}

		return $data[0];
	}	
	
	
	//根据uid取个数
	function get_count_by_uid($uid)
	{       
		//验证传入参数
		$uid = intval($uid);
		if(!$uid) throw new Exception("DB:uid错误！");
		//如果id是字符串，并且在where条件中使用 ：$id = string::esc_mysql($id);
			 
		$data = false;
		$key = CACHE_PREFIX_user_follow . "-get_count_by_uid-{$uid}";
		$mc = mem_cache::get_instance();
		$cache = $mc->get($key);
		if ($cache === false)
		{
			//处理where条件
			$where = " uid = {$uid} and status = 1 " ;	
			$data = $this->get_listcount($this->tbl_name, $where);
			$mc->set($key, serialize($data), 0, CACHE_EXPIRE_MIN_5);
		}
		else
		{
			$data = unserialize($cache);
		}

		return $data;
	}	
	
	
	//根据uid取列表
	function get_list_by_uid($uid,$p=1,$pcount=20)
	{       
		//验证传入参数
		$uid = intval($uid);
		$p = intval($p);
		$pcount = intval($pcount);
		if(!$uid) throw new Exception("DB:uid错误！");
		//如果id是字符串，并且在where条件中使用 ：$id = string::esc_mysql($id);
			 
		$data = false;
		$key = CACHE_PREFIX_user_follow . "-get_list_by_uid-{$uid}-{$p}-{$pcount}";
		$mc = mem_cache::get_instance();
		$cache = $mc->get($key);
		if ($cache === false)
		{
			//处理where条件
			$where = " uid = {$uid} and status = 1 " ;	
			$data = $this->get_alllist($this->tbl_name, $p, $pcount, "id desc ",$where);
			$mc->set($key, serialize($data), 0, CACHE_EXPIRE_MIN_5);
		}
		else
		{
			$data = unserialize($cache);
		}

		return $data;
	}	
	
	//根据uid取列表
	function get_uids_list_by_uid($uid)
	{       
		//验证传入参数
		$uid = intval($uid);
		if(!$uid) throw new Exception("DB:uid错误！");
		//如果id是字符串，并且在where条件中使用 ：$id = string::esc_mysql($id);
			 
		$data = false;
		$key = CACHE_PREFIX_user_follow . "-get_list_by_uid-{$uid}-{$p}-{$pcount}";
		$mc = mem_cache::get_instance();
		$cache = $mc->get($key);
		if ($cache === false)
		{
			//处理where条件
			$where = " uid = {$uid} and status = 1 " ;	
			$data = $this->get_alllist($this->tbl_name, "", "", "",$where,"","follow_uid");
			$mc->set($key, serialize($data), 0, CACHE_EXPIRE_MIN_5);
		}
		else
		{
			$data = unserialize($cache);
		}

		return $data;
	}	
	
	
		
	//添加一条数据
	function insert_user_follow(
		$uid,
		$follow_uid,
		$uid_is_be_followed
	)
	{
		global $config;
		//详细字段
		$ary = array();
		$ary["uid"] = $uid;
		$ary["follow_uid"] = $follow_uid;
		$ary["uid_is_be_followed"] = $uid_is_be_followed;
		$ary["status"] = 1;
		
		$ret = $this->insert($this->tbl_name, $ary,"",true);
		

		return $ret;
	}

	
	function update_user_follow(
		$uid,
		$follow_uid,
		$status="",
		$uid_is_be_followed=""
	)
	{
		$ary = array();
		if(strlen($status)>0)$ary["status"] = $status;
		if(strlen($uid_is_be_followed)>0)$ary["uid_is_be_followed"] = $uid_is_be_followed;
		
		
		$uid = intval($uid);
		$follow_uid = intval($follow_uid);
		
		$where = " uid = {$uid} and  follow_uid = $follow_uid " ;	
		
		return $this->update($this->tbl_name, $ary,$where);
		
	}
	
}

